#include "head.h"

/* Usage:  */
int main(void){
    resource_p rspb;
    //创建队列
    queue_t *p = create_queue();
    //创建和初始化子线程
    create_thread(&rspb, 4);
    //创建socket文件描述符
    int socket_fd;
    create_socket(&socket_fd, "8080", "192.168.232.128");

    

    //创建epoll对象
    int epoll_fd = epoll_create(1);
    //将socket_fd放入epoll监听
    add_epoll(epoll_fd ,socket_fd);

    while(1){
        //监听队列
        struct epoll_event list[5];
        int epoll_num = epoll_wait(epoll_fd, list, 10, -1);
        for(int i = 0; i < epoll_num; i++){
        int fd = list[i].data.fd;
        if(fd == socket_fd){//说明有客户端请求
        //建立连接
        int net_fd = accept(socket_fd, NULL, NULL);
        ERROR_CHECK(net_fd, -1, "accept_error");
        //将net_fd放入队列
        //加锁
        pthread_mutex_lock(&rspb.lock);
        add_queue( &rspb.queue , net_fd);
        //printf("%d\n", rspb.queue.front->data);
        //add_queue(p, net_fd);
        //rspb.queue = *p;
        //释放锁
        pthread_cond_broadcast(&rspb.cond);
        pthread_mutex_unlock(&rspb.lock);
        }
    }
    }
    return 0;
}


